
package br.inf.solutions.apresentacoes.ssp.control;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import br.com.vexillum.util.Return;
import br.inf.solutions.apresentacoes.ssp.model.Person;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.FunctionPerson;

@Service
@Scope("prototype")
public class PersonController extends BaseController<Person> {

	public PersonController() {
		super(Person.class);
	}

	public Return getPersonByCPF() {
		Return ret = new Return(true);
		
		String sql = "FROM " + classEntity.getSimpleName() + " WHERE cpf = '"
				+ entity.getCpf() + "' AND active = true";
		data.put("sql", sql);

		ret.concat(searchByHQL());

		return ret;
	}

	public Person searchPersonByRG() {
		
		Long rg = (Long) (data.get("rg") != null ? data.get("rg") : entity.getRg());
		
		return searchPersonByRG(rg);
	}
	
	public Person searchPersonByRG(Long rg) {
		Return ret = new Return(true);
		
		String sql = "FROM " + classEntity.getSimpleName() + " WHERE rg = '" + rg + "' AND active = true";
		data.put("sql", sql);

		ret.concat(searchByHQL());
		Person person = (Person) (ret.getList().isEmpty() ? null : ret.getList().get(0));
			
		return person == null ? person : (Person) person.cloneEntity();
	}
	
	
	@SuppressWarnings("unchecked")
	public Return researchPersonFunction(){
		Return ret = new Return(true);
		List<Person> lstPerson;
		List<FunctionPerson> lstFunctionPerson = new ArrayList<>();
		List<Long> ids =  (List<Long>) data.get("listaID");
		String sql;

			sql = "FROM  Person WHERE unity.id in (" +  ids.toString().substring(1,ids.toString().length()-1)  +") AND active = true ORDER BY post.id ASC";
			
			data.put("sql", sql);		
			ret.concat(searchByHQL());		
			lstPerson = (List<Person>) ret.getList();
			
			for (Person p : lstPerson) {	
				
				sql = "FROM FunctionPerson WHERE person.id = '" + p.getId() + "' AND active = true";
							
				data.put("sql", sql);
				ret.concat(searchByHQL());
				lstFunctionPerson = (List<FunctionPerson>) ret.getList();			
			}
			
			if(!lstFunctionPerson.isEmpty() ){ 
				ret.setList(lstFunctionPerson);		
			}
		return ret;	
	}
	
	public Return firstStep() {
		if(entity.getId() != null)
			return super.update();
		return super.save();
	}

	public Return secondStep() {
		return super.update();
	}

	public Return thirdStep() {
		return super.update();
	}

	public Return fourthStep() {
		return super.update();
	}

	public Return fifthStep() {
		return super.update();
	}
	
	public Return getListReportTest(){
		Return ret = new Return(true);
		
		String sql = "SELECT h.name functionPerson, h.name organItern, h.name unity, f.begin_date date, f.begin_date bolDateBegin"+
				" FROM function_person f, hierarchy_entity h where f.person_id = 1 and f.hierarchy_entity_id = h.id";
		
		data.put("sql", sql);
		ret.concat(searchByNativeSQL());
		
		return ret;
		
	}
}